package ${location}.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ${location}.exception.Asserts;
import ${location}.exception.CustomizeException;
import ${location}.mapper.${className}Mapper;
import ${location}.model.dto.${camelClassName}.${className}AddDto;
import ${location}.model.dto.${camelClassName}.${className}PageDto;
import ${location}.model.entities.${className};
import ${location}.model.vo.${camelClassName}.${className}Vo;
import ${location}.service.${className}Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author ${author}
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = @Lazy)
@Transactional(rollbackFor = {Exception.class, CustomizeException.class})
public class ${className}ServiceImpl extends ServiceImpl<${className}Mapper, ${className}> implements ${className}Service {

	@Override
	public ${className}Vo saveEntity(${className}AddDto dto) {
		${className} ${camelClassName} = new ${className}();
		BeanUtils.copyProperties(dto, ${camelClassName});
		save(${camelClassName});
		${className}Vo vo = new ${className}Vo();
		BeanUtils.copyProperties(${camelClassName}, vo);
		return vo;
	}

	@Override
	public ${idType} deleteEntityById(${idType} id) {
		if (!removeById(id)) {
			Asserts.error("无效id");
		}
		return id;
	}

	@Override
	public ${className}Vo updateEntityById(${idType} id, ${className}AddDto dto) {
		${className} ${camelClassName} = getById(id);
		if (${camelClassName} == null) {
			Asserts.error("无效id!");
		}
		BeanUtils.copyProperties(dto, ${camelClassName});
		updateById(${camelClassName});
		${className}Vo vo = new ${className}Vo();
		BeanUtils.copyProperties(getById(id), vo);
		return vo;
	}

	@Override
	public ${className}Vo getEntityById(${idType} id) {
		${className} ${camelClassName} = getById(id);
		if (${camelClassName} == null) {
			Asserts.error("无效id!");
		}
		${className}Vo vo = new ${className}Vo();
		BeanUtils.copyProperties(${camelClassName}, vo);
		return vo;
	}

	@Override
	public IPage<${className}Vo> pageQuery(${className}PageDto dto) {
		dto.checkPageSize();
		IPage<${className}> iPage = new Page<>(dto.getPage(), dto.getSize());

		LambdaQueryWrapper<${className}> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		${lwq}

		page(iPage, lambdaQueryWrapper);
		List<${className}Vo> vos = iPage.getRecords().stream().map(object -> {
			${className}Vo vo = new ${className}Vo();
			BeanUtils.copyProperties(object, vo);
			return vo;
		}).collect(Collectors.toList());

		IPage<${className}Vo> voPage = new Page<>();
		BeanUtils.copyProperties(iPage, voPage, "records");
		voPage.setRecords(vos);
		return voPage;
	}
}